#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define READ 0
#define WRITE 1
int main(int argc, char *argv[])   //argv[1]  文件名字       2：读还是写  
{
    int ok = 0;
    int fd = 0;
    char *filename;
    char buff[100], wchar[100];
    int ReadRecive = 0;
    int writeback = 0;
    int mode = 0;
    static char userdata[]="usr data!"; 
    printf(" argv[1] = %s\r\n", filename);
    if (argc >= 2)
    {
        filename = argv[1];
        mode = atoi(argv[2]);
        printf(" argv[1] = %s\n", filename);
    }
    else
    {
        printf(" not enough\n");
        return 0;
    }
    fd = open(filename, O_RDWR);
    if (fd < 0)
    {
        printf("open file fail \n ");
        return -1;
    }
    else
    {
        printf("open file succes \n ");
    }

    if (mode == READ)
    {
        ReadRecive = read(fd, buff, 10);
        if (ReadRecive < 0)
        {
            printf("Read %s  error \n ", filename);
        }
        else
        {
            printf("Read buff :%s\n", buff);
        }
    }
    else 
    {
        memcpy(wchar,userdata,sizeof(userdata));
        writeback = write(fd, wchar, 10);
        if (writeback < 0)
        {
            printf("write %s  error \n ", filename);
        }
        else
        {
            printf("%d\n", writeback);
        }
    }



    ok = close(fd);
    if (ok < 0)
    {
        printf("close %s  error \n ", filename);
    }
    else
    {
        printf("close %s  succes \n ", filename);
    }

    return 0;
}
